package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: wf
 * @Desc:  菜品
 * @Date: 2022-08-22 20:13:02
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;


    // 新增菜品
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {

        // 先存菜品信息
        this.save(dishDto);
        Long dishId = dishDto.getId();

        // 在存菜品口味信息
        List<DishFlavor> dishFlavorList = dishDto.getFlavors();

        dishFlavorList = dishFlavorList.stream().map(item -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(dishFlavorList);
    }

    // 根据id查看菜品详情
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        DishDto dishDto = new DishDto();
        Dish dish = this.getById(id);

        BeanUtils.copyProperties(dish, dishDto);

        LambdaQueryWrapper<DishFlavor> flavorLqw = new LambdaQueryWrapper<>();
        flavorLqw.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavorList = dishFlavorService.list(flavorLqw);

        dishDto.setFlavors(dishFlavorList);

        return dishDto;
    }

    // 修改菜品信息
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        // 先是删除原来的菜品口味数据
        LambdaQueryWrapper<DishFlavor> flavorLqw = new LambdaQueryWrapper<>();
        flavorLqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(flavorLqw);

        // 再添加菜品口味
        List<DishFlavor> dishFlavorList = dishDto.getFlavors().stream().map(item -> {

            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(dishFlavorList);

        // 修改菜品表数据
        this.updateById(dishDto);
    }

    @Override
    public List<DishDto> listWithFlavor(Dish dish) {

        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(dish.getCategoryId().toString()), Dish::getCategoryId, dish.getCategoryId());
        lqw.eq(Dish::getStatus,dish.getStatus());
        List<Dish> dishList = this.list(lqw);

        List<DishDto> dishDtoList = dishList.stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);

            LambdaQueryWrapper<DishFlavor> flavorLqw = new LambdaQueryWrapper<>();
            flavorLqw.eq(DishFlavor::getDishId, dishDto.getId());
            List<DishFlavor> flavorList = dishFlavorService.list(flavorLqw);
            dishDto.setFlavors(flavorList);

            return dishDto;
        }).collect(Collectors.toList());

        return dishDtoList;
    }
}
